package org.example.demo.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.example.demo.model.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/param")
@RestController
public class ParamController {
    @RequestMapping("/m1")  //传递单个参数
    public String m1(String name) {
        return "接收到的参数name:"+name;
    }

    @RequestMapping("/m2")  //传递多个参数
    public String m2(String name, Integer age) {  //建议使用包装类
        return "接收到的参数name:"+name+",age:"+age;
    }

    @RequestMapping("/m3")  //以对象进行传送
    public String m3(Person person) {  //建议使用包装类
        return "接收到的参数person:"+person.toString();
    }

    @RequestMapping("/m4")  //后端参数重命名
    public String m4(@RequestParam("name") String username) {  //采用name,是必须传递的
        return "接收到的参数name:"+username;
    }

    @RequestMapping("/m5")  //后端参数重命名
    public String m5(@RequestParam(value = "name",required = false) String username) {  //采用name,但不是必须传送的
        return "接收到的参数name:"+username;
    }

    @RequestMapping("/m6")  //传递数组
    public String m6(String[] arrayParam) {  //当我们的请求中,同一个参数有多个时.浏览器就会帮我们封装成一个数组
        return "接收到的参数arrayParam:"+ Arrays.toString(arrayParam)+",长度: "+arrayParam.length;
    }

    @RequestMapping("/m7")  //传递集合
    public String m7(@RequestParam(required = false) List<String> ListParam) {  //加上required = false就不是必传项
        return "接收到的参数ListParam:"+ ListParam+",长度: "+ListParam.size();
    }

    @RequestMapping("/m8")
    public String m8(@RequestBody Person person) {  //传递Json数据,需要使用@RequestBody
        return "接收到的参数person:" + person.toString();
    }

    @RequestMapping("/m9/{userId}/{name}")   //获取URL中的参数
    //public String m9(@PathVariable Integer userId,@PathVariable("name") String username) {  //改名,拿到的是name
    public String m9(@PathVariable Integer userId,@PathVariable String name) {  //可以获取一个和多个,注意顺序, 匹配
        return "userId:"+userId+",name:"+name;
        //return "userId:"+userId+",username:"+username;  //改名
    }

    @RequestMapping("/m10")  //文件上传
    public String m10(@RequestPart MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:/Server/"+file.getOriginalFilename()));  //上传后的文件保存到目录
        return "success!!";
    }

    @RequestMapping("/getCookie")   //获取cookie   可以一次性拿多个
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();  //拿到Cookie对象
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName()+":"+cookie.getValue());  //打印cookie的值
            }
        }

        //foreach写法
        /*
        if(cookies != null) {
            Arrays.stream(cookies).forEach(cookie -> {
                System.out.println(cookie.getName()+":"+cookie.getValue());
            });
        }*/
        return "获取cookie成功";
    }

    @RequestMapping("/getCookie2")   //spring 获取cookie的值  只能一次性拿一个
    public String getCookie2(@CookieValue String bite,@CookieValue String bbb) {
        return "获取cookie存储的值bite:"+bite+",bbb:"+bbb;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("username","caijun");
        return "success!!";
    }
    @RequestMapping("/getSession")  //获取Session
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession(false);  //没有值默认为true,如果没有session,就会创建一个session
        if (session != null) {
            String username = (String) session.getAttribute("username");
            return "登录用户:"+username;
        }
        return "session为空";
    }

    @RequestMapping("/getSession2")  //第二种获取Session
    public String getSession2(@SessionAttribute(required = false)  String username) {  //默认必传参数
        return "username:"+username;
    }
    @RequestMapping("/getSession3")
    public String getSession3(HttpSession request) {  //等同于:HttpSession session = request.getSession(true)
        String username = (String) request.getAttribute("username");
        return "登录用户:"+username;
    }

    @RequestMapping("/getHeader")  //获取Header
    public String getHeader(HttpServletRequest request) {
        String header = request.getHeader("User-Agent");
        return "header:"+header;
    }

    @RequestMapping("/getHeader2")  //spring获取Header
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "userAgent:"+userAgent;
    }


}
